package com.school.graduation.classification.view;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.school.graduation.R;
import com.school.graduation.classification.adapter.ViewPagerAdapter;
import com.school.graduation.classification.presenter.ClassIficationPresenter;
import com.school.graduation.common.base.BaseFragment;
import com.school.graduation.goods.view.FoundGoodDetailsActivityNew;
import com.school.graduation.goods.view.FoundGoodsActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ClassIficationFragment extends BaseFragment implements ImplClassIfication, View.OnClickListener {
    @Bind(R.id.classification_classification_viewpager)
    ViewPager viewPager1;
    @Bind(R.id.classification_electricAppliance_viewpager)
    ViewPager viewPager2;
    @Bind(R.id.fragment_classification_scroll)
    ScrollView scroll;
    @Bind(R.id.fragment_classification_topsearch)
    RelativeLayout topSearch;
    @Bind(R.id.classification_classification_viewpagerlayout)
    LinearLayout viewpagerlayout1;
    @Bind(R.id.classification_electricAppliance_viewpagerlayout)
    LinearLayout viewpagerlayout2;
    @Bind(R.id.fragment_classification_computer_1)
    TextView fragmentClassificationComputer1;
    @Bind(R.id.fragment_classification_computer_2)
    TextView fragmentClassificationComputer2;
    @Bind(R.id.fragment_classification_computer_3)
    TextView fragmentClassificationComputer3;
    @Bind(R.id.fragment_classification_computer_4)
    TextView fragmentClassificationComputer4;
    @Bind(R.id.fragment_classification_computer_5)
    TextView fragmentClassificationComputer5;
    @Bind(R.id.fragment_classification_computer_6)
    TextView fragmentClassificationComputer6;
    @Bind(R.id.fragment_classification_car_1)
    LinearLayout fragmentClassificationCar1;
    @Bind(R.id.fragment_classification_car_2)
    LinearLayout fragmentClassificationCar2;
    @Bind(R.id.fragment_classification_car_3)
    LinearLayout fragmentClassificationCar3;
    @Bind(R.id.fragment_classification_appliances_1)
    LinearLayout fragmentClassificationAppliances1;
    @Bind(R.id.fragment_classification_appliances_2)
    LinearLayout fragmentClassificationAppliances2;
    @Bind(R.id.fragment_classification_appliances_3)
    LinearLayout fragmentClassificationAppliances3;
    @Bind(R.id.fragment_classification_appliances_4)
    LinearLayout fragmentClassificationAppliances4;
    @Bind(R.id.fragment_classification_appliances_5)
    LinearLayout fragmentClassificationAppliances5;
    @Bind(R.id.fragment_classification_appliances_6)
    LinearLayout fragmentClassificationAppliances6;
    @Bind(R.id.slide_top_bar)
    LinearLayout slideTopBar;
    @Bind(R.id.stop_top_bar)
    LinearLayout stop_top_bar;
    private List<ImageView> dotList1;
    private List<ImageView> dotList2;
    private Map<Integer, String> types;
    private int statusBarHeight1 = -1; //状态栏高度
    private ClassIficationPresenter presenter;
    private LayoutInflater inflater;

    @Override
    public int getFragmentView() {
        return R.layout.fragment_classification;
    }

    @Override
    public void initData() {
        setTopXY();
        scrollListener();
        inflater = getActivity().getLayoutInflater();
        presenter = new ClassIficationPresenter(getContext(), this, inflater);
        presenter.setViewPager();
        initdot();
        setviewpagerListener();
        initTypes();
        setOnclickListener();
    }

    //设置Scroll滑动监听
    @TargetApi(Build.VERSION_CODES.M)
    public void scrollListener() {
        scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > 1000) {
                    topSearch.setAlpha(1);
                } else {
                    topSearch.setAlpha(scrollY / 1200f);
                }
            }
        });
    }

    //获取状态栏高度，将头部搜索下拉
    private void setTopXY() {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        topSearch.setPadding(topSearch.getPaddingLeft(), topSearch.getPaddingTop() + statusBarHeight1, topSearch.getPaddingRight(), topSearch.getPaddingBottom());
    }

    @Override
    public void setViewPager(ViewPagerAdapter adapter1, ViewPagerAdapter adapter2) {
        viewPager1.setAdapter(adapter1);
        viewPager2.setAdapter(adapter2);
    }

    private void initdot() {
        dotList1 = new ArrayList<ImageView>();
        dotList2 = new ArrayList<ImageView>();
        for (int i = 0; i < 2; i++) {
            ImageView imageView = new ImageView(getContext());
            if (i == 0) {
                imageView.setImageResource(R.drawable.focus);
            } else {
                imageView.setImageResource(R.drawable.normal);
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            params.setMargins(15, 20, 15, 20);
            viewpagerlayout1.addView(imageView, params);
            dotList1.add(imageView);
        }

        for (int i = 0; i < 2; i++) {
            ImageView imageView = new ImageView(getContext());
            if (i == 0) {
                imageView.setImageResource(R.drawable.focus);
            } else {
                imageView.setImageResource(R.drawable.normal);
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            params.setMargins(15, 20, 15, 20);
            viewpagerlayout2.addView(imageView, params);
            dotList2.add(imageView);
        }
    }

    private void setviewpagerListener() {
        viewPager1.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < 2; i++) {
                    if (position % dotList1.size() == i) {
                        dotList1.get(i).setImageResource(R.drawable.focus);
                    } else {
                        dotList1.get(i).setImageResource(R.drawable.normal);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager2.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < 2; i++) {
                    if (position % dotList2.size() == i) {
                        dotList2.get(i).setImageResource(R.drawable.focus);
                    } else {
                        dotList2.get(i).setImageResource(R.drawable.normal);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void initTypes() {
        types = new HashMap<Integer, String>();
        types.put(R.id.fragment_classification_computer_1, "笔记本");
        types.put(R.id.fragment_classification_computer_2, "平板电脑");
        types.put(R.id.fragment_classification_computer_3, "台式机");
        types.put(R.id.fragment_classification_computer_4, "网络设备");
        types.put(R.id.fragment_classification_computer_5, "电脑配件");
        types.put(R.id.fragment_classification_computer_6, "炫酷外设");
        types.put(R.id.fragment_classification_car_1, "自行车");
        types.put(R.id.fragment_classification_car_2, "电动车");
        types.put(R.id.fragment_classification_car_3, "摩托车");
        types.put(R.id.fragment_classification_appliances_1, "电视机");
        types.put(R.id.fragment_classification_appliances_2, "洗衣机");
        types.put(R.id.fragment_classification_appliances_3, "空调");
        types.put(R.id.fragment_classification_appliances_4, "冰箱");
        types.put(R.id.fragment_classification_appliances_5, "厨房电器");
        types.put(R.id.fragment_classification_appliances_6, "热水器");
        /*types.put(R.id.view_classification_appliances_1, "游戏机");
        types.put(R.id.view_classification_appliances_2, "kindle");
        types.put(R.id.view_classification_appliances_3, "移动电源");
        types.put(R.id.view_classification_appliances_4, "摄影发烧");
        types.put(R.id.view_classification_appliances_5, "mp4");
        types.put(R.id.view_classification_appliances_6, "耳机");
        types.put(R.id.view_classification_1,"手机数码");
        types.put(R.id.view_classification_2,"电脑办公");
        types.put(R.id.view_classification_3,"家用电器");
        types.put(R.id.view_classification_4,"服装鞋帽");
        types.put(R.id.view_classification_5,"个性化妆");
        types.put(R.id.view_classification_6,"运动户外");
        types.put(R.id.view_classification_7,"交通工具");
        types.put(R.id.view_classification_8,"珠宝配饰");
        types.put(R.id.view_classification_9,"玩具乐器");
        types.put(R.id.view_classification_10,"艺术古玩");
        types.put(R.id.view_classification_11,"技能服务");
        types.put(R.id.view_classification_12,"票务卡券");
        types.put(R.id.view_classification_13,"动漫周边");
        types.put(R.id.view_classification_14,"图书音像");
        types.put(R.id.view_classification_15,"家居家纺");
        types.put(R.id.view_classification_16,"工具建材");*/
    }

    private void setOnclickListener() {
        fragmentClassificationComputer1.setOnClickListener(this);
        fragmentClassificationComputer2.setOnClickListener(this);
        fragmentClassificationComputer3.setOnClickListener(this);
        fragmentClassificationComputer4.setOnClickListener(this);
        fragmentClassificationComputer5.setOnClickListener(this);
        fragmentClassificationComputer6.setOnClickListener(this);
        fragmentClassificationCar1.setOnClickListener(this);
        fragmentClassificationCar2.setOnClickListener(this);
        fragmentClassificationCar3.setOnClickListener(this);
        fragmentClassificationAppliances1.setOnClickListener(this);
        fragmentClassificationAppliances2.setOnClickListener(this);
        fragmentClassificationAppliances3.setOnClickListener(this);
        fragmentClassificationAppliances4.setOnClickListener(this);
        fragmentClassificationAppliances5.setOnClickListener(this);
        fragmentClassificationAppliances6.setOnClickListener(this);
        /*viewClassificationAppliances1.setOnClickListener(this);
        viewClassificationAppliances2.setOnClickListener(this);
        viewClassificationAppliances3.setOnClickListener(this);
        viewClassificationAppliances4.setOnClickListener(this);
        viewClassificationAppliances5.setOnClickListener(this);
        viewClassificationAppliances6.setOnClickListener(this);
        viewClassification1.setOnClickListener(this);
        viewClassification2.setOnClickListener(this);
        viewClassification3.setOnClickListener(this);
        viewClassification4.setOnClickListener(this);
        viewClassification5.setOnClickListener(this);
        viewClassification6.setOnClickListener(this);
        viewClassification7.setOnClickListener(this);
        viewClassification8.setOnClickListener(this);
        viewClassification9.setOnClickListener(this);
        viewClassification10.setOnClickListener(this);
        viewClassification11.setOnClickListener(this);
        viewClassification12.setOnClickListener(this);
        viewClassification13.setOnClickListener(this);
        viewClassification14.setOnClickListener(this);
        viewClassification15.setOnClickListener(this);
        viewClassification16.setOnClickListener(this);*/
    }

    @Override
    public void onClick(View v) {
        String item = types.get(v.getId());
        Bundle bundle = new Bundle();
        bundle.putBoolean("hot",false);
        bundle.putString("item", item);
        FoundGoodDetailsActivityNew.toDetail(context,bundle);
    }

    @OnClick({R.id.slide_top_bar,R.id.stop_top_bar})
    public void clickTopBar(View v){
        switch (v.getId()){
            case R.id.slide_top_bar:
                FoundGoodsActivity.jumpToFoundActivity(context,new Bundle());
                break;
            case R.id.stop_top_bar:
                FoundGoodsActivity.jumpToFoundActivity(context,new Bundle());
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
